package com.rui.common.config;

import com.rui.common.util.SysConst;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * swagger配置
 *
 * @author xr
 * @email xr@qq.com
 * @date 2020-01-01 00:00:00
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        /* 添加全局响应状态码 */
//        List<ResponseMessage> responseMessageList = new ArrayList<>();
//        Arrays.stream(HttpStatus.values()).forEach(httpStatus -> responseMessageList.add(
//                new ResponseMessageBuilder()
//                        .code(httpStatus.getCode())
//                        .message(httpStatus.getMsg())
//                        .responseModel(new ModelRef(httpStatus.getMsg()))
//                        .build()
//        ));
        return new Docket(DocumentationType.SWAGGER_2)
//                .globalResponseMessage(RequestMethod.GET, responseMessageList)
//                .globalResponseMessage(RequestMethod.POST, responseMessageList)
                .apiInfo(apiInfo())
                .groupName("接口文档")
                .forCodeGeneration(true)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(securitySchemes());
//                .securityContexts(securityContexts());
    }

    /* token */
    private List<ApiKey> securitySchemes() {
        List<ApiKey> apiKeyList = new ArrayList();
        apiKeyList.add(new ApiKey(SysConst.TOKEN, SysConst.TOKEN, "header"));
        return apiKeyList;
    }

    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts = new ArrayList<>();
        securityContexts.add(SecurityContext.builder().securityReferences(defaultAuth()).forPaths(PathSelectors.any()).build());
        return securityContexts;
    }

    private List<SecurityReference> defaultAuth() {
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[]{
                new AuthorizationScope("global", "accessEverything")
        };
        List<SecurityReference> securityReferences = new ArrayList<>();
        securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
        return securityReferences;
    }

    /**
     * 文档信息
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("API For code-generator")
                .description("代码生成接口文档")
                .version("1.0.0")
                .termsOfServiceUrl("http://aolaisi.com.cn/")
                .contact(new Contact("rui", "https://blog.csdn.net/xierui4056?type=blog", "405682903@qq.com"))
                .build();
    }
}
